package switchviz.gui.component;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;

import switchviz.core.ResourceManager;

public class JColorPanel extends JPanel implements ActionListener
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 2240611132921463826L;
	private JButton _buttcolorchooser = null;
	private JLabel _lblcolor = null;
	private String _descriptiontext = "";
	private float[] _color = null;
	
	public void setColor(float[] color)
	{
		if (color.length == 3)
		{
			_lblcolor.setBackground(new Color(color[0], color[1], color[2]));
			_color = color;
		}
//		else if(color.length == 4)
//		{
//			//alpha supposed to be in the first slot
//			_lblcolor.setBackground(new Color(color[1], color[2], color[3], color[0]));
//			_color = color;
//		}
	}
	
	public float[] getColor()
	{
		if (_color == null)
			_color = Color.white.getRGBColorComponents(null);
		return _color;
	}
	
	public JColorPanel(String descriptiontext, Dimension dimension)
	{
		_descriptiontext = descriptiontext;
		_buttcolorchooser = new JButton(_descriptiontext);
		_buttcolorchooser.addActionListener(this);
		_buttcolorchooser.setPreferredSize(dimension);
		_lblcolor = new JLabel();
		_lblcolor.setOpaque(true);
		_lblcolor.setBackground(new Color(getColor()[0], getColor()[1], getColor()[2]));
		_lblcolor.setBorder(BorderFactory.createLineBorder(Color.black, 2));
		this.setLayout(new BorderLayout());
		this.add(_buttcolorchooser, BorderLayout.WEST);
		this.add(_lblcolor, BorderLayout.CENTER);
		this.setMaximumSize(new Dimension(800, 20));
	}
	
	public JColorPanel(String buttontext)
	{
		this(buttontext, ResourceManager.getInstance().getConfigurationLabelDimension());
	}

	public void actionPerformed(ActionEvent evt) 
	{
		Color newColor = JColorChooser.showDialog(
                this,
                _descriptiontext,
                new Color(getColor()[0], getColor()[1], getColor()[2]));
		if(newColor != null)
			this.setColor(newColor.getRGBColorComponents(null));
	}
}
